import { clone, isUndefined } from "xe-utils";

export default {
  data() {
    return {
      loading: false,
      sort: null,
      order: null,
      paginationNotAdd: {
        page: 1,
        size: 10,
        total: 0,
        sizes: [10, 20, 50, 100],
        layout: "->, total, sizes, prev, pager, next, jumper",
      },
    };
  },
  methods: {
    getParamsNotAdd(params) {
      if (!isUndefined(params)) {
        this.resetPaginationParamsNotAdd();
      }

      // INCOMPATIBLE_FIELD 位于页面同级目录下 config.js , 用于字典类型和接口类型不匹配时自动转换
      const queryParams = clone(this.parkNameNo);
      const queryParamsKeys = Object.keys(queryParams);
      queryParamsKeys.forEach((key) => {
        const handler = this.INCOMPATIBLE_FIELD?.[key];
        if (handler) {
          queryParams[key] = handler[1](queryParams[key]);
        }
      });

      const { page, size } = this.paginationNotAdd;

      const baseParams = {};
      if (this.sort && this.order) {
        baseParams.sort = this.sort;
        baseParams.order = this.order;
      }
      Object.assign(baseParams, { pageIndex: page, length: size }, queryParams);
      return baseParams;
    },
    onSizeChangeNotAdd(size) {
      this.paginationNotAdd.size = size;
      this.getDataListNotAdd();
    },
    onCurrentChangeNotAdd(page) {
      this.paginationNotAdd.page = page;
      this.getDataListNotAdd();
    },
    onSortChangeNotAdd(params) {
      if (params.order) {
        this.sort = params.prop;
        this.order = params.order === "ascending" ? "asc" : "desc";
      } else {
        this.sort = null;
        this.order = null;
      }
      this.getDataListNotAdd();
    },
    resetPaginationParamsNotAdd() {
      this.paginationNotAdd.page = 1;
      this.paginationNotAdd.size = 10;
    },
  },
};
